/*
 * @Author: your name
 * @Date: 2021-11-24 11:43:33
 * @LastEditTime: 2022-01-17 15:48:01
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \vue-antd-admin-y\src\store\modules\account.js
 */
const getDefaultState = () => {
  return  {
    user: undefined,
    first: false,               //是否为第一次
    permissions: null,
    roles: null,
    routesConfig: null,
    header: localStorage.getItem(process.env.VUE_APP_HEADER_KEY)
  };
};
export default {
  namespaced: true,
  state : getDefaultState(),
  getters: {
    user: state => {
      if (!state.user) {
        try {
          const user = localStorage.getItem(process.env.VUE_APP_USER_KEY)
          state.user = JSON.parse(user)
        } catch (e) {
          console.error(e)
        }
      }
      return state.user
    },
    permissions: state => {
      if (!state.permissions) {
        try {
          const permissions = localStorage.getItem(process.env.VUE_APP_PERMISSIONS_KEY)
          state.permissions = JSON.parse(permissions)
          state.permissions = state.permissions ? state.permissions : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.permissions
    },
    roles: state => {
      if (!state.roles) {
        try {
          const roles = localStorage.getItem(process.env.VUE_APP_ROLES_KEY)
          state.roles = JSON.parse(roles)
          state.roles = state.roles ? state.roles : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.roles
    },
    routesConfig: state => {
      if (!state.routesConfig) {
        try {
          const routesConfig = localStorage.getItem(process.env.VUE_APP_ROUTES_KEY)
          state.routesConfig = JSON.parse(routesConfig)
          state.routesConfig = state.routesConfig ? state.routesConfig : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.routesConfig
    },
    first: state => {
      return state.first
    },
    getHeader:state => {
      return state.header?JSON.parse(state.header):{}
    }

  },
  mutations: {
    setUser(state, user) {
      state.user = user
      localStorage.setItem(process.env.VUE_APP_USER_KEY, JSON.stringify(user))
    },
    setPermissions(state, permissions) {
      state.permissions = permissions
      localStorage.setItem(process.env.VUE_APP_PERMISSIONS_KEY, JSON.stringify(permissions))
    },
    setRoles(state, roles) {
      state.roles = roles
      localStorage.setItem(process.env.VUE_APP_ROLES_KEY, JSON.stringify(roles))
    },
    setRoutesConfig(state, routesConfig) {
      state.routesConfig = routesConfig
      localStorage.setItem(process.env.VUE_APP_ROUTES_KEY, JSON.stringify(routesConfig))
    },
    setFirst(state, first = false) {
      state.first = first
    },
    setHeader(state, header) {
      let headerStr = JSON.stringify(header)
      state.header = headerStr
      localStorage.setItem( process.env.VUE_APP_HEADER_KEY , headerStr)
    },
    loginOutClear(state){
      // Object.assign(state, getDefaultState());
      state.header = ''
      state.permissions= null
      state.roles= null
      state.routesConfig= null
      
      window.localStorage.clear();           // 清空所有缓存

    }
  }
}
